package goid

import (
	"runtime"
	"strconv"
	"strings"
)

func GetPid() int {
	return int(getGID())
}

func getGID() int64 {
	b := make([]byte, 64)
	b = b[:runtime.Stack(b, false)]
	goidStr := strings.TrimPrefix(string(b), "goroutine ")
	goidStr = goidStr[:strings.Index(goidStr, " ")]
	gid, err := strconv.ParseInt(goidStr, 10, 64)
	if err != nil {
		return -1
	}
	return gid
}
